Repository navigation
Report the recursion limit when serializing deeply nested values - #5684
aviralgarg05 wants to merge 2 commits into
Conversation
`JSON.stringify` and `JsValue::to_json` recurse natively over the object graph, one native frame per level of nesting, and neither path consulted `check_runtime_limits`, so a deeply nested but acyclic value exhausted the native stack and aborted the process regardless of the configured `RuntimeLimits`. The cycle set only guards against cyclic graphs. Count both descents as host recursion through the `host_call_depth` counter, so the existing recursion-limit error is reported instead, and cover nested objects, nested arrays and `to_json` with regressions. Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
abdulhamid999
left a comment
There was a problem hiding this comment.
Clean approach — reusing Vm::host_call_depth and check_runtime_limits() via serialize_json_nested and to_json_inner avoids introducing a parallel recursion counter, and the regression tests cover both within-limit and over-limit nesting cleanly.
Two minor details worth noting:
- In
core/engine/src/value/conversions/serde_json.rs,to_json_innerincrementshost_call_depthfor every value including primitive leaf properties, whereasserialize_json_nestedonly increments on nestedJsObjects (objects/arrays). Guarding inside theJsVariant::Objectbranch would keepto_jsonandJSON.stringifydepth counting consistent (and avoid consuming depth on a top-level primitive). - In
core/engine/src/object/operations.rs(lines 460 and 516), decrements usecontext.vm.host_call_depth = context.vm.host_call_depth.saturating_sub(1);rather than-= 1for extra safety.
(As a quick related observation for a potential follow-up: internalize_json_property in json/mod.rs for JSON.parse(..., reviver) and JsValue::from_json in serde_json.rs also recurse natively per nesting level without consulting host_call_depth.)
to_json_inner charged every value, including primitive leaves, while serialize_json_nested only ever sees a JsObject. The two therefore reported the recursion limit at different depths, and a top-level primitive consumed a level for nothing. Return early for non-objects, and match operations.rs by decrementing with saturating_sub.
|
Thanks @abdulhamid999, both of those were right and I’ve applied them. |
|
Thanks for the quick update! Agreed on keeping |
This Pull Request fixes/closes #5637.
It changes the following:
SerializeJSONObject/SerializeJSONArrayas host recursion, soJSON.stringifyreports the configured recursion limit instead of exhausting the native stack and aborting the process.JsValue::to_json, which recursed throughto_json_innerwithout consulting the runtime limits either.to_json, and asserts that nesting within the limit is unaffected.Both paths recurse one native frame per level of nesting and the cycle set only guards against cyclic graphs, so a deep acyclic value had nothing to stop it. This follows #4699, which added
Vm::host_call_depthfor the equivalent accessor problem in #4535, rather than introducing a second mechanism.One behaviour note worth your call: with the default
recursion_limitof 512, a value nested deeper than that now reports the limit where it previously serialized, up to the depth at which the native stack gave out (measured in the issue at roughly 2.7k levels on a 2 MiB stack). I asked on the issue whether you would prefer serialization to have a separate, higher limit, and I am happy to change it.Testing:
cargo test -p boa_engine --libtwice, 1142 passed each timecargo test -p boa_engine --doc, 185 passedcargo make run-ci, which iscargo fmt -- --checkplus clippy with--all-features --all-targetsand--no-default-features